package ru.stepuk.modern;

import ru.stepuk.modern.nodes.Node;
import edu.uci.ics.jung.graph.Graph;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import ru.stepuk.modern.generators.RandomNumberGenerator;
import ru.stepuk.modern.nodes.Source;

/**
 *
 * @author ivan
 */
public class Model {

    private String name = "Untitled";
    private String description = "";
    private Graph<Node, Path> structure;
    private RandomNumberGenerator rng;
    private TimeUnit timeUnit = TimeUnit.SECOND;
    private String requestName = "требование";

    public RandomNumberGenerator getGenerator() {
        return rng;
    }

    public String getRequestName() {
        return requestName;
    }

    public void setRequestName(String requestName) {
        this.requestName = requestName;
    }

    public TimeUnit getTimeUnit() {
        return timeUnit;
    }

    public void setTimeUnit(TimeUnit timeUnit) {
        this.timeUnit = timeUnit;
    }

    public Model(Graph<Node, Path> structure, RandomNumberGenerator rng) {
        this.structure = structure;
        this.rng = rng;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Node getNodeByPath(Path path) {
        return structure.getDest(path);
    }

    public Node getNodeById(int id) {
        Iterator<Node> it = structure.getVertices().iterator();
        Node current = null;
        while (it.hasNext()) {
            current = it.next();
            if (current.getId() == id) {
                return current;
            }
        }
        return null;
    }

    public Collection<Node> getAllNodes() {
        return structure.getVertices();
    }

    public Collection<Source> getSources() {
        Collection<Source> ret = new ArrayList<Source>();
        Iterator<Node> it = structure.getVertices().iterator();
        while (it.hasNext()) {
            try {
                ret.add((Source) it.next());
            } catch (ClassCastException ex) {
                //
            }
        }
        return ret;
    }

    public Collection<Path> getOutcome(Node node) {
        return structure.getOutEdges(node);
    }

    public Collection<Path> getIncome(Node node) {
        return structure.getInEdges(node);
    }

    public Collection<Node> getNextNodes(Node node) {
        return structure.getSuccessors(node);
    }

    public Collection<Node> getPreviousNodes(Node node) {
        return structure.getPredecessors(node);
    }

    void reset() {
        name = "Untitled";
        description = "";
        Node[] dup = structure.getVertices().toArray(new Node[0]);
        for (Node n : dup) {
            structure.removeVertex(n);
        }


        timeUnit = TimeUnit.SECOND;
        requestName = "требование";
    }
}
